//
//  LGEditQuickReplyController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/28.
//

#import "LGEditQuickReplyController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
@interface LGEditQuickReplyController ()<UITextViewDelegate>

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UITextView *textView;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UIButton *deleteBtn;
/**   */
@property (nonatomic , strong)UIButton *saveButton;
/**   */
@property (nonatomic , strong)UIButton *saveButton2;
/**   */
@property (nonatomic , copy)NSString *placeHolder;

@end

@implementation LGEditQuickReplyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.placeHolder = @"请输入...";
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [self.view addSubview:self.baseView];
    [self.baseView addSubview:self.textView];
    [self.baseView addSubview:self.numberLabel];
    if(_replyId && _replyId.length>0){
        self.title = @"编辑快捷回复";
        [self.view addSubview:self.deleteBtn];
        [self.view addSubview:self.saveButton];
    }else{
        self.title = @"添加快捷回复";
        [self.view addSubview:self.saveButton2];
    }
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.baseView).offset(-viewPix(16));
    }];
}


//删除
-(void)deleteBtnAction{
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"删除快捷回复" content:@"确定删除该条内容吗？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/im/quickMessageDelete" parameters:@{@"ids":self.replyId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"editQuickReplyMessage" object:nil];
                [TooltipView showMessage:@"删除成功" offset:0];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:LGErrorMessage offset:0];
        }];
    }];
    
}

//保存
-(void)saveBtnAction{
    if(self.textView.text.length==0||[self.textView.text  isEqualToString:self.placeHolder]){
        [TooltipView showMessage:@"请输入快捷回复的内容" offset:0];
        return;
    }
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:@{@"content":self.textView.text}];
    if(_replyId && _replyId.length>0){
        [tempDic setValue:self.replyId forKey:@"id"];
    }
    [RequestUtil withPOST:@"/doctor/im/quickMessageSave" parameters:tempDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"editQuickReplyMessage" object:nil];
            [TooltipView showMessage:@"添加成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}


-(void)setContent:(NSString *)content{
    _content = content;
    self.textView.text = content;
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        self.textView.text = self.placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#9595A0"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:self.placeHolder]){
        self.textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSString *content = textView.text;
    if(content.length == 0||[textView.text isEqualToString:self.placeHolder]){
        self.textView.text = self.placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#9595A0"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            textView.text = (textView.text.length>500)?[textView.text substringToIndex:500]:textView.text;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
        }
    }else{
        textView.text = (textView.text.length>500)?[textView.text substringToIndex:500]:textView.text;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
    }
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _baseView.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(327));
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(280))];
        _textView.contentInset = UIEdgeInsetsMake(viewPix(10), viewPix(16), viewPix(10), viewPix(16));
        _textView.tintColor = [UIColor colorWithString:@"#666666"];
        _textView.textColor = [UIColor colorWithString:@"#999999"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.text = self.placeHolder;
        _textView.font = LGFont(16);
        _textView.delegate = self;
    }
    return _textView;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0/500" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _numberLabel;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithImage:@"快捷回复-删除" selectImage:@"快捷回复-删除"];
        _deleteBtn.frame = CGRectMake(viewPix(47), Screen_H-topBarHeight-viewPix(80), viewPix(128), viewPix(46));
        [_deleteBtn setImage:[UIImage imageNamed:@"快捷回复-删除"] forState:UIControlStateSelected];
        [_deleteBtn addTarget:self action:@selector(deleteBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

-(UIButton *)saveButton{
    if(!_saveButton){
        _saveButton = [UIButton buttonWithImage:@"快捷回复-保存" selectImage:@"快捷回复-保存"];
        _saveButton.frame = CGRectMake(Screen_W-viewPix(47)-viewPix(128), Screen_H-topBarHeight-viewPix(80), viewPix(128), viewPix(46));
        [_saveButton setImage:[UIImage imageNamed:@"快捷回复-保存"] forState:UIControlStateSelected];
        [_saveButton addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

-(UIButton *)saveButton2{
    if(!_saveButton2){
        _saveButton2 = [UIButton buttonWithTitle:@"保存" topMargin:Screen_H-topBarHeight-viewPix(80)];
        [_saveButton2 addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton2;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
